# -*- coding: utf-8 -*-
#   combo部件
# @Time    ： 2023/2/16  16:51
# @Auther  ： 徐良
# File     :  comboBoxs.py

from typing import List
from PySide6.QtCore import Signal
from PySide6.QtWidgets import QComboBox
from domainAndEnum.domainFunction import (combox_TypeList,
                            combox_WidgetGridList,
                            combox_AlignList,
                            combox_WidgetFormList)


class BaseCombo(QComboBox):
    """
    基础类
    """
    dataChanged = Signal(str)  # 自定义信号
    def __init__(self, parent=None):
        super().__init__(parent)


# region 数据类型、对齐方式、form编辑方式的控件， 网格编辑方式的控件，
class ComBoxType(BaseCombo):
    """
    数据类型
    """
    def __init__(self, parent=None):
        super().__init__(parent)
        self.addItems(combox_TypeList.copy())

class ComBoxAlign(BaseCombo):
    """
    对齐
    """
    def __init__(self, parent=None):
        super().__init__(parent)
        self.addItems(combox_AlignList.copy())

class ComBoxWidgetForm(BaseCombo):
    """
    用于表头的控件
    """
    def __init__(self, parent=None):
        super().__init__(parent)
        self.addItems(combox_WidgetFormList.copy())

class ComBoxWidgetGrid(BaseCombo):
    """
    用于表头的控件
    """
    def __init__(self, parent=None):
        super().__init__(parent)
        self.addItems(combox_WidgetGridList.copy())

# endregion


class Combox(BaseCombo):

    def __init__(self, nameList: List[str]=None, default_val: str = '', parent=None):
        super().__init__(parent)

        if nameList is None:
            self.addItems([])
        else:
            self.addItems(nameList)
            self.default_val = default_val
        self.currentIndexChanged.connect(self._on_index_changed)

    def _on_index_changed(self, index):
        """处理选项变化事件"""
        if index >= 0:
            self.dataChanged.emit(self.currentText())

    # 以下两个方法是关键，使QDataWidgetMapper能正确工作
    def currentText(self) -> str:
        """重写currentText以支持映射器"""
        return super().currentText()


    def setCurrentText(self, text: str) -> None:
        """重写setCurrentText以支持映射器"""
        super().setCurrentText(text)
        if text not in [self.itemText(i) for i in range(self.count())]:
            self.addItem(text)  # 如果值不在选项中，动态添加


class ComboxFree(QComboBox):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.Text = ''
        self.setStyleSheet("""
QComboBox {
    border: 2px solid #3498db;  /* 边框颜色为 #3498db */
    border-radius: 5px;
    padding: 5px;
    background-color: white;
}
QComboBox:focus {
    border: 2px solid #FFA500;  /* 获取焦点时边框颜色变为橘黄色 */
}
QComboBox::drop-down {
    subcontrol-origin: padding;
    subcontrol-position: top right;
    width: 20px;
    border-left: 2px solid #3498db;  /* 下拉按钮左侧边框颜色 */
}
QComboBox::down-arrow {
    image: url(:/arrow/resource/arrow/down.png);  /* 使用资源文件中的图标 */
    width: 16px;
    height: 16px;
}
QComboBox QAbstractItemView {
    border: 2px solid #3498db;  /* 下拉列表边框颜色 */
    selection-background-color: #e0e0e0;  /* 选中项背景颜色 */
}
        """)